package com.hitfm.iplay;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;

public class MusicInfoController {
	private static MusicInfoController mInstance = null;
	private static String[] prjs = new String[] { Media._ID, Media.TITLE, Media.ALBUM, Media.ARTIST, Media.ALBUM_ID, Media.DATA };
	private static String[] singerprjs = new String[] { Media._ID, Media.TITLE, Media.ALBUM, Media.ARTIST, Media.ALBUM_ID,
			Media.DATA, "count(*)" };

	private static MusicPlayerApp pApp = null;

	public static MusicInfoController getInstance(MusicPlayerApp app) {
		if (mInstance == null) {
			mInstance = new MusicInfoController(app);
		}
		return mInstance;
	}

	private MusicInfoController(MusicPlayerApp app) {
		pApp = app;
	}

	public MusicPlayerApp getMusicPlayer() {
		return pApp;
	}

	private Cursor query(Uri uri, String[] prjs, String selections, String[] selectArgs, String order) {
		ContentResolver resolver = pApp.getContentResolver();
		if (resolver == null) {
			return null;
		}
		return resolver.query(uri, prjs, selections, selectArgs, order);
	}

	public Cursor getAllSongs() {
		return query(Media.EXTERNAL_CONTENT_URI, prjs, Media.DURATION + " > " + 1000 * 60 * 2, null, Media.DEFAULT_SORT_ORDER);
	}

	public Cursor getAllSongsSortByArtist() {
		return query(Media.EXTERNAL_CONTENT_URI, singerprjs, Media.DURATION + " > " + 1000 * 60 * 2 + ") group by "
				+ Media.ARTIST_KEY + " -- (", null, Media.ARTIST_KEY);
	}

	public Cursor getAllSongsByArtist(String artist) {
		return query(Media.EXTERNAL_CONTENT_URI, prjs, " " + Media.ARTIST + " = \"" + artist + "\" and " + Media.DURATION + " > "
				+ 1000 * 60 * 2, null, Media.DEFAULT_SORT_ORDER);
	}

	public int deleteByURL(String url) {
		ContentResolver resolver = pApp.getContentResolver();
		if (resolver == null) {
			return 0;
		}
		return resolver.delete(Media.EXTERNAL_CONTENT_URI, " " + Media.DATA + " = \"" + url + "\"", null);
	}

	public Cursor getSingleSongByURL(String url) {
		ContentResolver resolver = pApp.getContentResolver();
		if (resolver == null) {
			return null;
		}
		return resolver.query(Media.EXTERNAL_CONTENT_URI, null, " " + Media.DATA + " = \"" + url + "\"", null, null);
	}

	public int updateSongInfo(String url, String filename, String songname, String singer, String album) {
		ContentResolver resolver = pApp.getContentResolver();
		if (resolver == null) {
			return 0;
		}
		ContentValues values = new ContentValues(4);
		values.put(Media.DISPLAY_NAME, filename);
		values.put(Media.TITLE, songname);
		values.put(Media.ARTIST, singer);
		values.put(Media.ALBUM, album);
		return resolver.update(Media.EXTERNAL_CONTENT_URI, values, " " + Media.DATA + " = \"" + url + "\"", null);
	}

	public Cursor getAllSongFolder(String url) {
		ContentResolver resolver = pApp.getContentResolver();
		if (resolver == null) {
			return null;
		}
		return resolver.query(Media.EXTERNAL_CONTENT_URI, null, " " + Media.DATA + " like'" + url + "%' and " + Media.DATA
				+ " like'%.mp3' and " + Media.DURATION + " > " + 1000 * 60 * 2, null, null);
	}

}
